#ifndef POLYGONDATA_H
#define POLYGONDATA_H

#include <QMap>

#include "./db/dbfabric.h"

#include <QStandardItem>
#include <QSharedPointer>

//drawable builders
#include "./drawables/builders/drawablebuilder.h"
#include "./drawables/builders/loopdrawablebuilder.h"
#include "./drawables/builders/picketdrawablebuilder.h"
#include "./drawables/builders/emfdrawablebuilder.h"

//info dock builders
#include "./infodocks/builders/infodockelementsbuilder.h"
#include "./infodocks/builders/loopinfodockbuilder.h"
#include "./infodocks/builders/picketinfodockbuilder.h"
#include "./infodocks/builders/emfinfodockbuilder.h"

//standart item model builder
#include "./standarditem/builders/standartitembuilder.h"


#define COAXIAL "coaxial"
#define USUAL  "usual"
#define INVALID_EMF_VALUE -1000000

class ElementIterator;

struct PolygonItem
{
    QSharedPointer<DrawableBuilder> drawableBuilder;
    QSharedPointer<InfoDockElementsBuilder> infodockBuilder;
    QSharedPointer<StandartItemBuilder> standardItemBuilder;
    QSharedPointer<QSqlRelationalTableModelWithReconnection> model;
    QString sqlFilter;
    QVector <QSqlRecord> dbRecord;
    double valueEmf;

public:
    PolygonItem()
    {
        sqlFilter = "";
        valueEmf = INVALID_EMF_VALUE;
    }
};

class PolygonData
{
private:
    int _maxId;
    QMap <int, PolygonItem*> _pickets;
    QMap <int, PolygonItem*> _loops;
    QMap <int, PolygonItem*> _emfs;

    PolygonData();
    static QSharedPointer<PolygonData> _instance;

    void _loadLoops(QString idPolygon);
    void _loadPickets(QString idPolygon);

    PolygonItem* _findPolygonItemById(int id);

    void _loadEmfByPicket(int id);
    void _loadEmfByLoop(int id);

    void _setMeasurementType(QString idPolygon);

public:
    static QSharedPointer<PolygonData> instance();

    QString measurementType;

    void _setValueEmf(int id, double value);

    bool load(QString idPolygon);
    bool loadEmf(int id);
    bool loadEmfByTime(int id, QString time);
    bool loadAverageEmf(QProgressDialog *progressHandler);

    void clear();

    QMap<QString, Drawable *> getDrawable(int id);
    QLayout* getInfoDock(int id, void* data = 0);
    QString getSqlFilter(int id);
    QVector <QSqlRecord> getSqlRecord(int id);
    double getValueEmf(int id);

    QVector<QStandardItem *> getStandardItem(int id);

    QSharedPointer<ElementIterator> createLoopIterator();
    QSharedPointer<ElementIterator> createPicketIterator();
    QSharedPointer<ElementIterator> createEmfIterator();

    int getPicketsCount();

    friend class LoopIterator;
    friend class PicketIterator;
    friend class EmfIterator;
};

#endif // POLYGONDATA_H
